const express = require("express");

const app = express();

const path = require("path");
// 导入处理数据编码的依赖模块(npm)
const bodyParser = require("body-parser");
const cors = require("cors");

//将静态资源托管，这样才能在浏览器上直接访问预览图片或则html页面
let staticPath = path.join(__dirname, "public");
app.use(express.static(staticPath));

// 解析 application/x-www-form-urlencoded 类型的数据
app.use(bodyParser.urlencoded({ extended: false }));
// 解析 application/json 类型数据
app.use(bodyParser.json());

app.use(cors());

app.use("/user", require("./routers/user"));
app.use("/home", require("./routers/home"));
app.use("/detail", require("./routers/detail"));
app.use("/search", require("./routers/search"));
app.use("/bookmark", require("./routers/bookmark"));
app.use("/follow", require("./routers/follow"));
app.use("/like", require("./routers/like"));
app.use("/news", require("./routers/comments"));

const { createServer } = require("http");
const { Server } = require("socket.io");
const httpServer = createServer(app);
const io = new Server(httpServer, {
    cors: {
        //解决跨域问题
        origin: "*",
        methods: ["GET", "POST"],
    },
});
// app.use(require("./routers/comments")(io));

httpServer.listen(3000, () => {
    console.log("后台服务启动了");
    console.log("http://localhost:3000");
    console.log("http://127.0.0.1:3000");
});

// app.listen(3000, () => {
//     console.log("后台服务启动了");
//     console.log("http://localhost:3000");
//     console.log("http://127.0.0.1:3000");
// });
